package review.single_;

public class SingleTon01 {
    public static void main(String[] args) {
        //通过方法可以获取对象
        GirlFriend instance = GirlFriend.getInstance();
        System.out.println(instance);

        GirlFriend instance2 = GirlFriend.getInstance();
        System.out.println(instance2);
    }
}

//只能有一个女朋友
class GirlFriend {
    private String name;

    private static GirlFriend gf = new GirlFriend("marry");

    //单例模式-饿汉式
    //1.将构造器私有化
    //2.在类的内部直接创建对象（static修饰）
    //3.提供一个公共的static方法，返回gf对象
    public GirlFriend(String name) {
        this.name = name;
    }

    public static GirlFriend getInstance() {
        return gf;
    }
}
